
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import Router from 'vue-router'
import './router/permission'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import request from '@/network/request'
import Vuex from 'vuex'
import store from './store/index.js'
import 'font-awesome/css/font-awesome.css'
import Contextmenu from 'vue-contextmenujs'
import GlobalLoading from './components/GlobalLoading.vue'
 
import websocket from './util/websocket.js'
import  VueQuillEditor from 'vue-quill-editor'
// require styles 引入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

Vue.use(VueQuillEditor)
Vue.prototype.$websocket = websocket
Vue.component('GlobalLoading', GlobalLoading)
// 注册全局组件
Vue.component('global-loading', GlobalLoading)

// 创建全局实例
const loadingInstance = new Vue({
  components: {
    GlobalLoading
  },
  template: '<global-loading ref="loading"></global-loading>'
}).$mount()

// 将全局实例挂载到 Vue.prototype 上
Vue.prototype.$loading = loadingInstance.$refs.loading

// 将全局实例添加到页面中
document.body.appendChild(loadingInstance.$el)

// 使用示例：
// 显示加载
// this.$loading.show();
// 隐藏加载
// this.$loading.hide();

Vue.use(Contextmenu)
// 在组件中调用显示菜单
// this.$contextmenu(options:MenuOptions);
// 鼠标点击或滚轮自动销毁, 也可手动销毁
// this.$contextmenu.destroy();

// 去除浏览器默认菜单
// event.preventDefault();

// 主题颜色 rgba(33, 196, 145, 1)
Vue.use(Vuex)

Vue.config.productionTip = false

// 引入 axios
Vue.prototype.$axios = request

// 引入 Element
Vue.use(ElementUI)
// 图片回显统一路径
const imageUrl = process.env.VUE_APP_BASE_IMAGE_URL
Vue.prototype.$imageUrl = imageUrl

// 图片回显统一路径
const videoUrl = process.env.VUE_APP_BASE_VIDEO_URL
Vue.prototype.$videoUrl = videoUrl

// websocket请求路径
const websocketUrl = process.env.VUE_APP_BASE_WSS_URL
Vue.prototype.$websocketUrl = websocketUrl

const IP= process.env.VUE_APP_BASE_URL
// 图片上传路径 返回值图片id
const uploadUrl = IP + '/file/isFile'
Vue.prototype.$uploadUrl = uploadUrl

// axios请求url前缀
request.defaults.baseURL = IP

request.interceptors.request.use((config) => {
  config.headers.Authorization = window.sessionStorage.getItem('token')
  return config
})

request.interceptors.response.use((config) => {
  if (config.data.code === 20003 ||
    config.data.code === 20004 ||
    config.data.code === 20008 ||
    config.data.code === 20011 ||
    config.data.code === 20012 ||
    config.data.code === 20001 ||
    config.data.code === 20005 ||
    config.data.code === 20009 ||
    config.data.code === 20007 ||
    config.data.code === 100022
  ) {
    // window.sessionStorage.removeItem('token')
    // window.sessionStorage.removeItem('userId')
    window.sessionStorage.clear()
    router.push('/')
  }
  if (config.data.code !== 200) {
    if (config.data.data.errCode === 20011) {
      // window.sessionStorage.removeItem('token')
      // window.sessionStorage.removeItem('userId')
      // window.sessionStorage.removeItem('avatarUrl')
      window.sessionStorage.clear()
      location.reload()
    }
  }
  return config
})

// 解决vue-router@3.0版本及以上回调形式已经改成promise api的形式了
// 返回的是一个promise，如果路由地址跳转相同, 且没有捕获到错误，
// 3.0以下版本则不会出现以下警告！！！，因路由回调问题…
const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
